package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

   @Reference
   private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private IStrategyTagService strategyTagService;
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")StrategyDetailQuery qo, Model model){
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "strategyDetail/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail,String tags){
        strategyDetailService.saveOrUpdate(strategyDetail, tags);

        return new AjaxResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id){
        strategyDetailService.delete(id);
        strategyContentService.delete(id);
        return new AjaxResult();
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state,Long id){
        strategyDetailService.changeState(state,id);
        return new AjaxResult();
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        //共享给页面所有的大攻略
       model.addAttribute("strategies",strategyService.selectAll());
       //共享给页面所有的攻略主题
       model.addAttribute("themes",strategyThemeService.selectAll());
       //根据id查询strategyDetail共享给页面
        model.addAttribute("strategyDetail",strategyDetailService.selectDetailById(id));
        model.addAttribute("tags",strategyTagService.selectTagsByDetailId(id));
       return "strategyDetail/input";
    }

    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId){
       return  strategyCatalogService.getCatalogByStrategyId(strategyId);
    }

}
